package com.yjr.gulimall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yjr.gulimall.product.entity.AttrEntity;
import com.yjr.gulimall.product.service.AttrService;
import com.yjr.gulimall.product.vo.AttrGroupWithAttrsVO;
import com.yjr.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjr.common.utils.PageUtils;
import com.yjr.common.utils.Query;

import com.yjr.gulimall.product.dao.AttrGroupDao;
import com.yjr.gulimall.product.entity.AttrGroupEntity;
import com.yjr.gulimall.product.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper=new QueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)){
            queryWrapper.and(obj->obj.eq("attr_group_id",key).or()
                    .like("attr_group_name",key));
        }
        if(catelogId==0){
            //查询全部
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return  new PageUtils(page);
        }
        else{

            queryWrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper);
            return  new PageUtils(page);
        }

    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrgroupWithAttrsByCatelogId(Long catelogId) {

        //查询分类下的所有属性
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if(!CollectionUtils.isEmpty(groupEntities)){
            List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS = groupEntities.stream().map(attrGroupEntity -> {
                AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
                BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVO);
                List<AttrEntity> attrRelationList = attrService.getAttrRelationList(attrGroupEntity.getAttrGroupId());
                attrGroupWithAttrsVO.setAttrs(attrRelationList);
                return attrGroupWithAttrsVO;
            }).collect(Collectors.toList());

            return attrGroupWithAttrsVOS;
        }

        return null;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId,Long catalogId) {

        return baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);

    }

}